package com.iweb.tmall.controller;

import com.iweb.tmall.entity.Admin;
import com.iweb.tmall.entity.BaseEntity;
import com.iweb.tmall.global.ResponseData;
import com.iweb.tmall.mapper.AdminMapper;
import com.iweb.tmall.param.AdminParam;
import com.iweb.tmall.param.BaseParam;
import com.iweb.tmall.service.AdminService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "管理员控制器")
@RestController
@RequestMapping("/api/admin")
public class AdminController extends BaseController<AdminParam, Admin>{
    /*
     * 父类中定义两个位置的数据类型泛型P 和泛型E
     * 子类在继承的时候
     * 指定泛型P ->AdminParam
     * 指定泛型E ->Admin
     * */
    @Autowired(required = false)//在控制器中声明业务层对象,然后是同@Autowired注解让SpringBoot自动装配这个对象
    private AdminService adminService;
    @Override
    public ResponseData list(AdminParam param) {
        return adminService.getList(param);
    }

    @Override
    public ResponseData update(Admin entity) {
        return adminService.updateById(entity);
    }

    @Override
    public ResponseData delete(Integer id) {
        return adminService.deleteById(id);
    }

    @Override
    public ResponseData add(Admin entity) {
        return adminService.save(entity);
    }

    @Override
    public ResponseData view(Integer id) {
        return adminService.getById(id);
    }

    @PostMapping("/login2")
    public ResponseData login2(Admin admin,HttpServletRequest request, HttpServletResponse response){
        return adminService.login2(admin,request,response);
    }

    /**
     *
     * @param admin 接受登录请求中的username和password参数
     * @param request   会由Springboot自动传入request对象，操作session对象
     * @param response  会由Springboot自动传入response对象，操作cookie对象
     * @return
     */
    @PostMapping("/login")
    public ResponseData login(Admin admin,HttpServletRequest request, HttpServletResponse response){
        return adminService.login(admin,request,response);
    }


}
